package com.lx.websockt.biz;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.lx.websockt.web.WebsocktDo;
import org.springframework.stereotype.Component;

import java.io.IOException;

//@Component
public class DefaultMsgSender implements IMsgSender {

    protected ObjectMapper objectMapper = new ObjectMapper();

    {
        // 解决手动json转换日期报错问题
        objectMapper.registerModule(new JavaTimeModule());
    }



    @Override
    // 默认群发
    public void send(WebsocktDo.Msg msg) throws Exception {
        WebsocktDo.getOnlines().forEach((id, session)->{
            try {
                String json = objectMapper.writeValueAsString(msg);
                // getBasicRemote()
                session.getBasicRemote().sendText(json);
            } catch (IOException e) {
                throw new RuntimeException("消息发送失败", e);
            }
        });
    }
}
